package com.xiaomei.manager.web.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaomei.manager.dao.JPushRecordMapper;
import com.xiaomei.manager.dao.UserMapper;
import com.xiaomei.manager.model.JPushRecord;
import com.xiaomei.manager.model.User;
import com.xiaomei.manager.service.IJpushRecordService;
import com.xiaomei.manager.service.impl.JPushRecordServiceImpl;
import com.xiaomei.manager.web.dto.OrderAndUser;
import com.xiaomei.manager.web.dto.req.JPushRecordDto;
import com.xiaomei.manager.web.dto.req.MessageDto;
import com.xiaomei.manager.web.jpush.JpushUtil;
import com.xiaomei.manager.web.sms.HttpSender;

@Controller
@RequestMapping("/push")
public class JPushRecordController {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private IJpushRecordService iJpushRecordService;
		@RequiresPermissions("push:view")
	    @RequestMapping(method = RequestMethod.GET)
	    public String showCreateForm(Model model) {	        
	       // model.addAttribute("exp", new Experience());
	       //model.addAttribute("op", "发送");
			model.addAttribute("jpushRecord", new JPushRecordDto());
	       return "push/list";
	    }

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody 
	public String GetValidCode(JPushRecordDto jpushRecord,HttpServletRequest request, HttpServletResponse response) {
		String sex = jpushRecord.getSex();
		String context = jpushRecord.getContext();
		List<User> selectByFemale = userMapper.selectByFemale(sex);	
		push(selectByFemale,context);

		JPushRecord jrp=new JPushRecord();
		jrp.setContent(context);
		jrp.setMessageState(JpushUtil.STATE_SUCCESS);
		jrp.setMessageType(JpushUtil.NEW_JPUSH_RECORD);
		jrp.setPushTime(new Timestamp(System.currentTimeMillis()));
		if (sex==null || sex.isEmpty()) {
			iJpushRecordService.JpushAllUser(jrp);
		} else if (sex.equals("女")) {
			iJpushRecordService.JpushByTag("woman", jrp);
		} else if (sex.equals("男")) {
			iJpushRecordService.JpushByTag("man", jrp);
		}
		return new String("SMS success！");
	}

	private void push(List<User> userlist,String  context) {
		for (int i = 0; i < userlist.size(); i++) {
			try {
				String userCode = userlist.get(i).getUserCode();
				JPushRecord jrp=new JPushRecord();
				jrp.setUserCode(userCode);
				jrp.setContent(context);
				jrp.setMessageState(JpushUtil.STATE_SUCCESS);
				jrp.setMessageType(JpushUtil.NEW_JPUSH_RECORD);
				jrp.setPushTime(new Timestamp(System.currentTimeMillis()));
				jrp.setReadState("0");
				iJpushRecordService.insertMessage(jrp);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
		}
	}	
}
